<template>
	<view class="content">
		<view class="question-page-header">
			<mynavBar :title="title" :gps="gps" :bgcolor="bgColor"></mynavBar>
		</view>
		<view class="content-body">
			<view class="content-body-list">
				<view class="body-list-one" @click="loadimg">
					<view class="list-one-left" style="display: flex;align-items: center;">
						<span>头像</span>
						<image :src="user.avatar?.url" mode=""
							style="width: 50rpx;height: 50rpx; border-radius: 100%;margin-left: 30rpx;"></image>
					</view>
					<view class="list-one-right">
						<span style="margin-right: 30rpx;">编辑头像</span>
						<uni-icons type="right" size="24"></uni-icons>
					</view>
				</view>
				<view class="body-list-one" @click="changeName">
					<view class="list-one-left">
						<span>昵称</span>
					</view>
					<view class="list-one-right">
						<input type="text" v-model="user.nickname" placeholder="输入昵称" style="margin-right: 30rpx;"
							:disabled="true">
						<uni-icons type="right" size="24"></uni-icons>
					</view>
				</view>
				<view class="body-list-one" @click="changeMobile">
					<view class="list-one-left">
						<span>手机</span>
					</view>
					<view class="list-one-right">
						<input type="text" v-model="user.mobile" placeholder="授权获取手机号" style="margin-right: 30rpx;"
							:disabled="true">
						<uni-icons type="right" size="24"></uni-icons>
					</view>
				</view>
				<view class="body-list-one">
					<view class="list-one-left">
						<span>性别</span>
					</view>
					<view class="list-one-right">
						<view class="one-right-boy" style="margin-right: 30rpx;" @click="changeSex('男')">
							<view class="right-boy-box"
								style="width: 30rpx; height: 30rpx;border-radius: 100%; border: 2rpx solid #000000;display: flex;justify-content: center;align-items: center;margin-right: 16rpx;"
								:style="user.sex=='男'?' border: 4rpx solid #000000;':''">
								<view class="boy-box-chidern"
									style="width: 22rpx; height: 22rpx; border-radius: 100%;background-color: #FDB827;"
									v-if="user.sex=='男'">
								</view>
							</view>
							<span>男生</span>
						</view>
						<view class="one-right-boy" @click="changeSex('女')" style="margin-right: 30rpx;">
							<view class="right-boy-box"
								style="width: 30rpx; height: 30rpx;border-radius: 100%; border: 2rpx solid #000000;display: flex;justify-content: center;align-items: center;margin-right: 16rpx;"
								:style="user.sex=='女'?' border: 4rpx solid #000000;':''">
								<view class="boy-box-chidern"
									style="width: 22rpx; height: 22rpx; border-radius: 100%;background-color: #FDB827;"
									v-if="user.sex=='女'">
								</view>
							</view>
							<span>女生</span>
						</view>
						<uni-icons type="right" size="24"></uni-icons>
					</view>
				</view>
				<view class="body-list-one" @click="changeEmail">
					<view class="list-one-left">
						<span>邮箱</span>
					</view>
					<view class="list-one-right">
						<input type="text" v-model="user.email" placeholder="电子邮箱" style="margin-right: 30rpx;"
							:disabled="true">
						<uni-icons type="right" size="24"></uni-icons>
					</view>
				</view>

				<view class="body-list-one" @click="loadBackgroundImg" v-if="user.is_answer =='已通过'">
					<view class="list-one-left" style="display: flex;align-items: center;">
						<span>背景</span>
						<image :src="user.background_img?.url" v-if="user.background_img!=null" mode=""
							style="width: 50rpx;height: 50rpx;margin-left: 30rpx;"></image>
					</view>
					<view class="list-one-right">
						<span style="margin-right: 30rpx;">编辑答主背景</span>
						<uni-icons type="right" size="24"></uni-icons>
					</view>
				</view>

				<view class="body-list-one" @click="loadVideo" v-if="user.is_answer =='已通过'">
					<view class="list-one-left" style="display: flex;align-items: center;">
						<span>简介视频</span>
					</view>
					<view class="list-one-right">
						<span style="margin-right: 30rpx;">上传简介视频</span>
						<uni-icons type="right" size="24"></uni-icons>
					</view>
				</view>
			</view>
		</view>
		<!-- 弹出输入框 -->
		<!-- 输入框示例 -->
		<uni-popup ref="inputDialog" type="dialog">
			<uni-popup-dialog ref="inputClose" mode="input" title="编辑昵称" :value="user.nickname" placeholder="请输入您的昵称"
				@confirm="dialogInputConfirm"></uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="inputDialogtwo" type="dialog">
			<uni-popup-dialog ref="inputClosetwo" mode="input" title="编辑手机号" :value="user.mobile" placeholder="请输入您的手机号"
				@confirm="dialogInputConfirmtwo"></uni-popup-dialog>
		</uni-popup>
		<uni-popup ref="inputDialogthree" type="dialog">
			<uni-popup-dialog ref="inputClosethree" mode="input" title="编辑邮箱" :value="user.email" placeholder="请输入您的邮箱"
				@confirm="dialogInputConfirmthree"></uni-popup-dialog>
		</uni-popup>
		<!-- 定位的保存按钮 -->
		<view class="postion-btn" @click="submitUser">
			<span>确认保存</span>
		</view>
	</view>
</template>

<script>
	import mynavBar from '@/components/mynavBar/mynavBar.vue'
	export default {
		components: {
			mynavBar
		},
		data() {
			return {
				title: '个人资料',
				gps: '',
				bgColor: 'rgba(165, 42, 42, 0)',
				user: {},
				video_id: ''
			};
		},
		methods: {
			//保存提交按钮
			submitUser() {
				this.$store.dispatch("remote/mutation", {
					operation: 'update_user',
					where: {
						id: {
							_eq: uni.getStorageSync('userInfo').id
						}
					},
					_set: {
						avatar_id: this.user.avatar_id,
						nickname: this.user.nickname,
						mobile: this.user.mobile,
						email: this.user.email,
						sex: this.user.sex,
						background_img_id: this.user.background_img_id,
					},
					field_string: 'id sex wx_mini_openid mobile email nickname avatar{url} avatar_id user_interest{id user_user user_fans_user} background_img{url} profile tag_list attach_data '
				}).then(res => {
					if (this.video_id != '' &&
						this.user.video == null) {
						this.$store.dispatch('remote/mutation', {
							operation: 'insert_video',
							objects: [{
								video_id: this.video_id,
								user_user: uni.getStorageSync('userInfo').id
							}]
						})
						console.log("新增");
					} else if (this.video_id != '' &&
						this.user.video != null) {
						this.$store.dispatch('remote/mutation', {
							operation: 'update_video',
							where: {
								user_user: {
									_eq: uni.getStorageSync('userInfo').id
								}
							},
							_set: {
								video_id: this.video_id
							}
						})
						console.log("修改");
					}
					uni.showToast({
						title: '修改资料成功',
						duration: 1000,
						icon: "success"
					});
					uni.setStorageSync('userInfo', res.returning[0])
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/me/me'
						})
					}, 1000)
					console.log("修改成功", res)
				})
			},
			//所有弹出框部分
			changeName() {
				this.$refs.inputDialog.open()
			},
			dialogInputConfirm(e) {
				console.log('编辑昵称完成', e)
				this.user.nickname = e
			},
			changeMobile() {
				this.$refs.inputDialogtwo.open()
			},
			dialogInputConfirmtwo(e) {
				let test = /^1[3-9]\d{9}$/
				if (test.test(e)) {
					console.log('编辑手机号完成', e)
					this.user.mobile = e
				} else {
					uni.showToast({
						title: '手机号格式错误',
						duration: 1000,
						icon: "error"
					});
				}

			},
			changeEmail() {
				this.$refs.inputDialogthree.open()
			},
			dialogInputConfirmthree(e) {
				let test =
					/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/

				if (test.test(e)) {
					console.log('编辑邮箱完成', e)
					this.user.email = e
				} else {
					uni.showToast({
						title: '邮箱格式错误',
						duration: 1000,
						icon: "error"
					});
				}

			},
			//图片上传函数
			async loadimg() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: async (res) => {
						if (this.user?.avatar) {
							this.user.avatar.url = res.tempFiles[0].path
						} else {
							this.user.avatar = {}
							this.user.avatar.url = res.tempFiles[0].path
						}
						this.user.avatar_id = await this.up_image_id(res.tempFiles[0])
						console.log("图片上传函数", this.user.avatar_id);
					}
				})
			},

			//图片上传函数
			async loadBackgroundImg() {
				uni.chooseImage({
					count: 1, //默认9
					sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图，默认二者都有
					sourceType: ['album'], //从相册选择
					success: async (res) => {
						if (this.user?.background_img) {
							this.user.background_img.url = res.tempFiles[0].path
						} else {
							this.user.background_img = {}
							this.user.background_img.url = res.tempFiles[0].path
						}
						this.user.background_img_id = await this.up_image_id(res.tempFiles[0])
						console.log("图片上传函数", this.user.background_img_id);
					}
				})
			},
			//视频上传函数
			async loadVideo() {
				var that = this
				uni.chooseVideo({
					sourceType: ['camera', 'album'],
					success: async function(res) {
						let src = {
							path: res.tempFilePath
						}
						await that.up_video_id(src)
					}
				});
			},
			//上传视频
			async up_video_id(data) {
				this.$store.dispatch('app/upload', {
					method: "uvideo",
					isloading: false,
					file: data
				}).then(res => {
					this.video_id = res.videoId
				})
			},
			//上传图片
			async up_image_id(data) {
				console.log('---', data);
				return this.$store.dispatch("app/upload", {
					method: "uimage",
					isloading: false,
					file: data
				}).then(res => {
					console.log("res.imageId....................", res.imageId)
					return res.imageId
				}).catch(error => {
					console.log('----', error);
				})
			},
			changeSex(val) {
				this.user.sex = val
			},
			getData() {
				this.$store.dispatch("remote/query", {
					model: 'user',
					where: {
						id: {
							_eq: uni.getStorageSync('userInfo').id
						}
					},
					field_string: 'id sex mobile email nickname avatar{url} avatar_id is_answer video{id video{url}} user_interest{id user_user user_fans_user} background_img{url} profile tag_list attach_data '
				}).then(res => {
					console.log("获取用户信息", res)
					this.user = JSON.parse(JSON.stringify(res.data))
				})
			}
		},
		onLoad: function(option) {
			//#ifdef MP-WEIXIN
			let arr = wx.getMenuButtonBoundingClientRect()
			this.gps = arr.top + ''
			console.log("胶囊位置信息", arr);
			//#endif
			this.getData()
		},
		onPageScroll(e) {
			//获取距离顶部距离
			const scrollTop = e.scrollTop;
			console.log("高度：", scrollTop)
			// 根据高度改变样式
			if (scrollTop < 99) {
				this.bgColor = 'rgba(247, 247, 247, 0)'

			} else if (scrollTop >= 99 && scrollTop < 150) {
				this.bgColor = 'rgba(247, 247, 247, 0.5)'
			} else if (scrollTop >= 150) {
				this.bgColor = 'rgba(247, 247, 247, 1)'
			}
		},
	}
</script>

<style lang="scss">
	.content {
		width: 100%;
		min-height: 100vh;
		background-color: #f4f4f4;

		.question-page-header {
			width: 100%;
			height: 343rpx;
		}

		.postion-btn {
			width: 455rpx;
			height: 70rpx;
			border-radius: 80rpx;
			opacity: 1;
			text-align: center;
			background: #F09500;
			font-family: Alibaba PuHuiTi 2.0;
			font-size: 24rpx;
			font-weight: bold;
			line-height: 70rpx;
			letter-spacing: 0px;
			color: #FFFFFF;
			position: fixed;
			bottom: 50rpx;
			left: 50%;
			transform: translateX(-50%);
		}

		.content-body {
			width: 100%;
			box-sizing: border-box;
			padding: 0 27rpx;
			margin-top: -140rpx;

			.content-body-list {
				width: 100%;
				border-radius: 15rpx;
				background-color: #fff;

				.body-list-one {
					width: 100%;
					height: 90rpx;
					border-width: 0px 0px 1px 0px;
					border-style: solid;
					border-color: #EEEEEE;
					box-sizing: border-box;
					padding: 0 29rpx 0 26rpx;
					display: flex;
					justify-content: space-between;
					align-items: center;

					.list-one-left {
						font-family: Alibaba PuHuiTi 2.0;
						font-size: 24rpx;
						font-weight: 500;
						line-height: 40rpx;
						letter-spacing: 0rpx;
						color: #000000;
					}

					.list-one-right {
						height: 100%;
						font-family: Alibaba PuHuiTi 2.0;
						font-size: 24rpx;
						font-weight: 500;
						text-align: right;
						letter-spacing: 0rpx;
						display: flex;
						align-items: center;
						color: #666666;

						.one-right-boy {
							font-family: Alibaba PuHuiTi 2.0;
							font-size: 24rpx;
							font-weight: 500;
							text-align: right;
							letter-spacing: 0rpx;
							display: flex;
							align-items: center;
							color: #666666;
						}
					}
				}
			}
		}
	}
</style>